<template>
    <view class="goods-evaluate-container">
        <z-paging 
            ref="paging" 
            class="evaluate-list"
            v-model="goodsEvaluateList"
            :scroll-with-animation="true"
            :default-page-size="params.page_size"
            @query="handlerPullRefreshAndMore"
            @scroll="handleScroll"
        >
            <!-- 头部区域开始 -->
            <template #top>

                <!-- 过滤tab区域开始 -->
                <view class="filter-tab-wrap">
                    <view class="filter-tab-item" @click="selectFilterTabs(index)" v-for="(item, index) in tabList" :key="index">
                        <text class="filter-tab-txt" :class="{active: currentTabIndex == index}">{{ item.text }}</text>
                        <text class="icon iconfont icon-shangjiantou active" v-if="currentTabIndex == index"></text>
                        <text class="icon iconfont icon-xiajiantou1" v-else></text>
                    </view>
                </view>
                <!-- 过滤tab区域结束 -->


                <!-- 过滤内容区域开始 -->
                <view class="filter-wrap" v-if="currentTabIndex != -1">
                    <!-- 过滤 -->
                    <template v-if="currentTabIndex == 0">
                        <view class="filter-item" v-for="(item, index) in filterList" :key="index" @click="handlerChangeFilter(item.value)">
                            <text class="filter-txt" :class="{active: filterValue == item.value}">{{ item.text }}</text>
                            <text class="icon iconfont icon-duigou" :class="{active: filterValue == item.value}" v-if="filterValue == item.value"></text>
                        </view>
                    </template>

                    <!-- 排序 -->
                    <template v-else-if="currentTabIndex == 1">
                        <view class="filter-item" v-for="(item, index) in sortList" :key="index" @click="handlerChangeSort(item.value)">
                            <text class="filter-txt" :class="{active: sortValue == item.value}">{{ item.text }}</text>
                            <text class="icon iconfont icon-duigou" :class="{active: sortValue == item.value}" v-if="sortValue == item.value"></text>
                        </view>
                    </template>

                    <!-- 综合筛选 -->
                    <template v-else-if="currentTabIndex == 2">
                        <view class="composite-filter-wrap">
                            <scroll-view scroll-y class="scroll-view">
                                <!-- 商品选项 -->
                                <view class="sku-wrap">
                                    <view class="sku-list">
                                        <view class="sku-item" v-for="(item, index) in goodsOptionList" :key="index">
                                            <template v-if="item.value">
                                                <view class="sku-title">{{ item.name }}</view>
                                                <view class="sku-content">
                                                    <template v-for="(value, index) in item.values" :key="index">
                                                        <text 
                                                            class="sku-txt ellipsis-1"
                                                            :class="selectSkuTexts.includes(item.name+'：'+value) ? 'active' : ''"
                                                            @click="handlerChangeCompositeFilter(item.name, value)">
                                                            {{ value }}
                                                        </text>
                                                    </template>
                                                </view>
                                            </template>
                                        </view>
                                    </view>
                                </view>
                            </scroll-view>

                            <view class="btn-wrap">
                                <text class="btn reset" @click="handlerReset">重置</text>
                                <text class="btn confirm" @click="handlerConfirm">确定</text>
                            </view>
                        </view>
                    </template>
                </view>
                <!-- 过滤内容区域结束 -->

            </template>

            <!-- 内容区域开始 -->
            <view class="evaluate-wrap">
                <!-- 骨架屏 -->
                <view class="evaluate-skeleton" v-if="isShowSkeleton" v-for="(item, index) in 3" :key="index">
                    <uv-skeletons :loading="isShowSkeleton" :skeleton="skeleton"></uv-skeletons>
                </view>

                <view class="evaluate-item" v-for="(item, index) in goodsEvaluateList" :key="index">
                    <!-- 用户信息区域开始 -->
                    <view class="user-wrap">
                        <view class="left">
                            <image class="user-avatar" v-if="item.avatar_url != ''" :src="global.FILE_DOMAIN + item.avatar_url"/>
                            <image class="user-avatar" v-else src="/static/images/avatar.png"/>
                            <view class="info">
                                <view class="username">{{ item.username ? item.username : '匿名买家'}}</view>
                                <view class="create_time">{{ item.create_time }}</view>
                            </view>
                        </view>
                        <view>
                            <uni-rate :readonly="true" size="18" v-model="item.composite_score"/>
                        </view>
                    </view>
                    <!-- 用户信息区域结束 -->

                    <view class="goods-wrap">
                        <text>{{ item.goods_sku_text }}</text>
                    </view>

                    <!-- 评价内容区域 -->
                    <view class="content">
                        <text class="txt">{{ item.contents }}</text>
                        <view class="image-wrap">
                            <view class="image-item" v-for="(image_url, index) in item.image_url_items" :key="index">
                                <image
                                    class="img"
                                    :src="global.FILE_DOMAIN + image_url"
                                    mode="scaleToFill"
                                />
                            </view>
                        </view>
                    </view>

                    <!-- 商家回复区域 -->
                    <view class="reply-wrap">
                        <text class="title">商家回复</text>
                        <text class="content">感谢亲的支持!欢迎下次光临!</text>
                    </view>

                    <!-- 操作区域开始 -->
                    <view class="option-wrap">
                        <view class="option-item">
                            <text class="icon iconfont icon-fenxiang"></text>
                            <text class="txt">分享</text>
                        </view>
                        <view class="option-item">
                            <text class="icon iconfont icon-pinglun"></text>
                            <text class="txt">评论</text>
                        </view>
                        <view class="option-item" @click="handlerUseful(item)" :style="item.useful == 1 ? 'color: #008000;' : ''">
                            <text class="icon iconfont icon-duigou1"></text>
                            <text class="txt">有用</text>
                        </view>
                    </view>

                </view>
            </view>
            <!-- 内容区域结束 -->

            <!-- 加载更多提示 -->
            <template #loadingMoreNoMore>
                <view :style=" safeAreaInsets.bottom > 0 ? 'padding-bottom: ' + safeAreaInsets.bottom + 'px': ''" class="no-more" v-if="pageTotal == params.page && pageTotal > 1">我也是有底线的！</view>
            </template>
    
        </z-paging>

        <!-- 返回顶部 -->
		<TridentFreshBackTop @backTop="backTop" :isShowBackTop="isShowBackTop"/>
    </view>
</template>


<script setup lang="ts">
import { ref, inject, reactive } from "vue"
import { reqGoodsEvaluateListAPI, reqGoodsEvaluateTotalAPI, reqGoodsEvaluateEditAPI } from '@/apis/goodsEvaluate'
import { reqGetGoodsOptionListAPI } from '@/apis/goods'



// ------------------ 定义全局变量开始 ------------------
// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()

// 获取页面参数
const query = defineProps({
    "goods_id": {
        type: String,
        default: ''
    }
})

// 请求参数
const params = reactive({
    goods_id: query.goods_id,
    goods_sku_id: 0,
    goods_sku_text: '',
    is_append: 'all',
    label_id: 0,
    is_image: 'all',
    composite_score: 0,
    sort_field: 'id',
    sort_type: 'desc',
    page: 1,
    page_size: 10
})


// 定义商品评价列表
const goodsEvaluateList:any = ref([])

// 商品评价数量
const total = ref(0)

// 总页数
const pageTotal = ref(0)

// 全局属性
const global:any = inject('global')

const paging = ref()
// ------------------ 定义全局变量结束 ------------------



// ------------------ 定义骨架变量开始 ------------------
// 定义是否显示骨架
const isShowSkeleton = ref(false)

// 定义骨架参数
const skeleton = reactive([
    {
      type: 'flex',
      num: 1,
      style: 'padding:25rpx 25rpx 0 25rpx;',
      children: [
        {
          type: 'avatar',
          num: 1,
          style: 'marginRight: 15rpx;'
        },
        {
          type: 'line',
          num: 2,
          style: 'width: 40%'
        },
      ]
    },
    {
        type: 'flex',
        num: 1,
        style: 'padding:25rpx;',
        children: [
            {
                type: 'line',
                num: 2,
            }
        ]
    },
    // 图片区域
    {
        type: 'flex',
        num: 1,
        style: 'padding:0 25rpx;justify-content: space-evenly;',
        children: [
            {
                type: 'custom',
                num: 1,
                style: 'width: 150rpx;height:150rpx;'
            },
            {
                type: 'custom',
                num: 1,
                style: 'width: 150rpx;height:150rpx;'
            },
            {
                type: 'custom',
                num: 1,
                style: 'width: 150rpx;height:150rpx;'
            },
            {
                type: 'custom',
                num: 1,
                style: 'width: 150rpx;height:150rpx;'
            },
        ]
    },
    20,
    // 商家回复
    {
        type: 'flex',
        num: 1,
        style: 'padding:0 25rpx;',
        children: [
            {
                type: 'custom',
                num: 1,
                style: 'height:50rpx;width:200rpx;margin-right:20rpx',
            },
            {
                type: 'line',
                num: 1,
                style: 'width: 70%',
            }
        ]
    },
    20,
    // 操作
    {
        type: 'flex',
        num: 1,
        style: 'padding:0 25rpx;justify-content: space-between;',
        children: [
            {
                type: 'custom',
                num: 1,
                style: 'height:50rpx;width:200rpx;',
            },
            {
                type: 'custom',
                num: 1,
                style: 'height:50rpx;width:200rpx;',
            },
            {
                type: 'custom',
                num: 1,
                style: 'height:50rpx;width:200rpx;',
            },
        ]
    },
    20
])
// ------------------ 定义骨架变量结束 ------------------



// 清除商品列表参数
const clearQueryParams = () => {
    params.goods_sku_id = 0
    params.is_append = 'all'
    params.label_id = 0
    params.is_image = 'all'
    params.composite_score = 0

	// params.sort_field = 'id'
	// params.sort_mode = 'desc'
	params.page = 1
	goodsEvaluateList.value = []
}



// ------------------ 商品评价选项功能开始 ------------------
// 当前 Tab 索引
const currentTabIndex = ref(-1)

// 定义商品选项
const goodsOptionList:any = ref([])

// 定义选项数据
const tabList = ref([
    { text: '全部', value: 0 },
    { text: '默认排序', value: 1 },
    { text: '筛选', value: 2 },
])

// 选择 tab
const selectFilterTabs = async (index:any) => {
    paging.value.scrollToTop()
    currentTabIndex.value = index
    // 选中全部
    if (index == 0) {
        await Promise.all([
            // 获取有图片的评价数量
            getGoodsEvaluateTotal({goods_id: query.goods_id, is_image: '1', mode: 'image'}),
            // 获取有追加的评价数量
            getGoodsEvaluateTotal({goods_id: query.goods_id, is_append: '1', mode: 'append'}),
            // 获取好评数量
            getGoodsEvaluateTotal({goods_id: query.goods_id, composite_score: 5, mode: 'praise'})
        ])
    } else if (index == 2) {
        const result:any = await reqGetGoodsOptionListAPI({goods_id: query.goods_id})
        const goodsOptionItems = result.data.goods_option_items != null ? result.data.goods_option_items : []
        goodsOptionItems.forEach((item:any) => {
            item.values = item.value.split(',')
            goodsOptionList.value.push(item as never)
        });
    }
}
// ------------------ 商品评价选项功能结束 ------------------



// ------------------ 商品评价过滤功能开始 ------------------
// 筛选值
const filterValue = ref(0)

// 过滤条件
const filterList = ref([
    { text: '全部', value: 0 },
    { text: '视频/图片(0)', value: 1 },
    { text: '追评(0)', value: 2 },
    { text: '好评(0)', value: 3 },
])

// 处理过滤
const handlerChangeFilter = (value:any) => {

    clearQueryParams()
    
    filterValue.value = value
    tabList.value[0].text = filterList.value[value].text

    if (value == 1) { // 视频/图片
        params.is_image = '1'
    } else if (value == 2) { // 追评
        params.is_append = '1'
    } else if (value == 3) { // 好评
        params.composite_score = 5
    }

	// 当tab切换或搜索时，可以通过paging.value.reload()刷新整个列表
    paging.value.reload()
    currentTabIndex.value = -1
}
// ------------------ 商品评价过滤功能结束 ------------------



// ------------------ 商品评价排序功能开始 ------------------
// 排序值
const sortValue = ref(0)

// 排序条件
const sortList = ref([
    { text: '默认排序', value: 0 },
    { text: '评分高到低', value: 1 },
    { text: '评分低到高', value: 2 },
])

// 处理排序
const handlerChangeSort = (value:any) => {
    sortValue.value = value
    tabList.value[1].text = sortList.value[value].text
    if (value == 1) { // 评分高到低
        params.sort_field = 'composite_score'
        params.sort_type = 'desc'
    } else if (value == 2) { // 评分低到高
        params.sort_field = 'composite_score'
        params.sort_type = 'asc'
    } else {
        params.sort_field = 'id'
	    params.sort_type = 'desc'
    }

    params.page = 1
	goodsEvaluateList.value = []
    // 当tab切换或搜索时，可以通过paging.value.reload()刷新整个列表
    paging.value.reload()
    currentTabIndex.value = -1
}
// ------------------ 商品评价排序功能结束 ------------------



// ------------------ 商品评价综合筛选开始 ------------------
// 定义选中的sku
const selectSkuTexts = ref([])

// 定义选中的sku
const selectSkuList = ref([])
const handlerChangeCompositeFilter = (name:any, value:any) => {

    // 查询要删除的数据
    let delIndex = -1
    selectSkuList.value.forEach((item:any, index) => {
        if (item.name == name) {
            delIndex = index
        }
    })

    // 删除已存在的数据
    if (delIndex != -1) {
        selectSkuList.value.splice(delIndex, 1)
        selectSkuTexts.value.splice(delIndex, 1)
    }

    selectSkuTexts.value.push(name+'：'+value as never)
    selectSkuList.value.push({name, value} as never)
}

// 重置数据
const handlerReset = () => {
    selectSkuTexts.value = []
    selectSkuList.value = []
}

// 确定
const handlerConfirm = () => {
    let goodsSkuText = selectSkuTexts.value
    params.goods_sku_text = goodsSkuText.reverse().join(' ')
    currentTabIndex.value = -1
    goodsOptionList.value = []

    params.page = 1
	goodsEvaluateList.value = []
    // 当tab切换或搜索时，可以通过paging.value.reload()刷新整个列表
    paging.value.reload()
    currentTabIndex.value = -1
}
// ------------------ 商品评价综合筛选结束 ------------------



// -------------------- 返回顶部功能开始 --------------------
// 定义是否显示返回顶部
const isShowBackTop = ref(false)

// 返回顶部
const backTop = () => {
    paging.value.scrollToTop()
}

// 当滚动触发时，调用事件用于显示、隐藏返回顶部图标
const handleScroll = (e:any) => {
    isShowBackTop.value = e.detail.scrollTop > 300 ? true : false
}
// -------------------- 返回顶部功能结束 --------------------



// -----------------下拉刷新 + 加载更多开始 -----------------
const handlerPullRefreshAndMore = (pageNo:any, pageSize:any) => {
    params.page = pageNo
    getGoodsEvaluateList()
}
// -----------------下拉刷新 + 加载更多结束 -----------------



// ------------------ 获取评价列表功能开始 ------------------
const getGoodsEvaluateList = async () => {
    // 显示骨架
    if (params.page == 1) {
        isShowSkeleton.value = true
    }

	// 请求数据
	const result:any = await reqGoodsEvaluateListAPI(params)
	let goodsComments = result.data.goods_comments != null ? result.data.goods_comments : []
    total.value = result.data.total
    pageTotal.value = result.data.page_total
    paging.value.complete(goodsComments);

    // 隐藏骨架
    isShowSkeleton.value = false
}

// 获取评价数量
const getGoodsEvaluateTotal = async (queryParam:any) => {
    // 请求数据
	const result:any = await reqGoodsEvaluateTotalAPI(queryParam)
    if (queryParam.mode == 'image') {
        filterList.value[1].text = '视频/图片('+ result.data.total +')'
    } else if (queryParam.mode == 'append') {
        filterList.value[2].text = '追评('+ result.data.total +')'
    } else if (queryParam.mode == 'praise') {
        filterList.value[3].text = '好评('+ result.data.total +')'
    }
}
// ------------------ 获取评价列表功能结束 ------------------



// ---------------------- 有用功能开始 ----------------------
const handlerUseful = async (item:any) => {
    let useful = item.useful == 1 ? 0 : 1
    await reqGoodsEvaluateEditAPI({id: item.id, useful})
    
    item.useful = useful

    uni.showToast({
        title: '编辑评价成功',
        duration: 1000
    })
}
// ---------------------- 有用功能结束 ----------------------
</script>

<style lang="scss">
.goods-evaluate-container {
    .evaluate-list {
        // 过滤区域
        .filter-tab-wrap {
            height: 120rpx;
            line-height: 120rpx;
            display: flex;
            justify-content: space-evenly;
            background-color: #fff;
            border-bottom: 2rpx solid #ccc;
            .filter-tab-item {
                .filter-tab-txt {
                    font-size: 32rpx;
                }
                .icon {
                    font-size: 28rpx;
                    color: #ccc;
                    padding-left: 10rpx;
                }
                .active {
                    color: #008000;
                }
            }
        }

        .filter-wrap {
            background-color: rgba(0, 0, 0, 0.7);
            height: 100vh;
            .filter-item {
                background-color: #fff;
                height: 80rpx;
                line-height: 80rpx;
                padding: 0 20rpx;

                display: flex;
                justify-content: space-between;
                border-bottom: 2rpx solid #ccc;
                &:last-child {
                    border-bottom: none;
                }
                .icon {
                    font-size: 40rpx;
                }
            }
            .composite-filter-wrap {
                background-color: #fff;
                .scroll-view {
                    height: calc(100vh - 500rpx);
                    .sku-wrap {
                        .sku-list {
                            .sku-item {
                                .sku-title {
                                    font-size: 32rpx;
                                    text-align: center;
                                    padding:20rpx 0;
                                    color: #008000;
                                    border-bottom: 2rpx dashed #ccc;
                                    font-weight: bold;
                                }
                                .sku-content {
                                    display: flex;
                                    flex-wrap: wrap;
                                    padding-top: 10rpx;
                                    .sku-txt {
                                        border: 2rpx solid #ccc;
                                        padding: 4rpx;
                                        margin: 14rpx  6rpx;
                                        border-radius: 2rpx;
                                        width: 46%;
                                    }
                                    .active {
                                        color: #fff;
                                        background-color: #008000;
                                    }
                                }
                            }
                        }
                    }
                }

                .btn-wrap {
                    height: 100rpx;
                    line-height: 100rpx;
                    margin-top: 20rpx;
                    text-align: center;
                    border-top: 2rpx solid #ccc;
                    .btn {
                        padding: 10rpx 80rpx;
                        border-radius: 4rpx;
                    }
                    .reset {
                        margin-right: 40rpx;
                        border: 2rpx solid #ccc;
                        color: #ccc;
                    }
                    .confirm {
                        background-color: #008000;
                        color: #fff;
                    }
                }
            }
            .active {
                color: #008000;
            }
        }

        .evaluate-wrap {
            padding-bottom: 20rpx;
            .evaluate-skeleton {
                background-color: #fff;
                margin: 10rpx;
                border-radius: 8rpx;
            }
            .evaluate-item {
                background-color: #fff;
                margin: 0 10rpx;
                border-radius: 10rpx;
                padding: 20rpx;
                margin-top: 15rpx;
                .user-wrap {
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    padding-bottom: 10rpx;
                    border-bottom: 2rpx dashed #ccc;
                    .left {
                        display: flex;
                        align-items: center;
                        .user-avatar {
                            width: 100rpx;
                            height: 100rpx;
                            border-radius: 50rpx;
                        }
                        .info {
                            padding-left: 10rpx;
                            .create_time {
                                color: #ccc;
                                font-size: 24rpx;
                            }
                        }
                    }
                }

                // 商品信息区域
                .goods-wrap {
                    padding: 10rpx 0;
                    color: #ccc;
                    font-size: 26rpx;
                }

                .content {
                    font-size: 26rpx;
                    // 评价图片
                    .image-wrap {
                        margin: 20rpx 0;
                        display: flex;
                        .image-item {
                            width: 25%;
                            height: 160rpx;
                            padding-right: 10rpx;
                            &:nth-child(4n) {
                                padding-right: 0;
                            }
                            .img {
                                width: 100%;
                                height: 100%;
                            }
                        }
                    }
                }

                // 商家回复
                .reply-wrap {

                    .title {
                        font-size: 30rpx;
                        font-weight: bold;
                        padding-right: 10rpx;
                    }
                }

                // 操作区域
                .option-wrap {
                    margin-top: 20rpx;
                    display: flex;
                    justify-content: space-between;
                    .option-item {
                        .icon {
                            font-size: 36rpx;
                            padding-right: 10rpx;
                        }
                        .txt {
                            font-size: 32rpx;
                        }
                    }
                }
            }
        }

        // 没有更多区域
        .no-more {
            text-align: center;
            padding-bottom: 20rpx;
            color: #ccc;
            &::before {
                content: '-------------';
                margin-right: 10rpx;
            }
            &::after {
                content: '-------------';
                margin-right: 10rpx;
            }
        }
    }
}
</style>